Component({
  data: {
    selected: 0,
    showCreatePanel: false,
    list: [
      {
        "pagePath": "pages/index/index",
        "text": "首页",
        "icon": "🏠",
        "selectedIcon": "🏡"
      },
      {
        "pagePath": "pages/discussion/index", 
        "text": "讨论",
        "icon": "📄",
        "selectedIcon": "📝"
      },
      {
        "pagePath": "",
        "text": "", 
        "icon": "➕",
        "selectedIcon": "➕",
        "isCreate": true
      },
      {
        "pagePath": "pages/goods/index",
        "text": "商城",
        "icon": "🛍️",
        "selectedIcon": "💴"
      },
      {
        "pagePath": "pages/usercenter/index",
        "text": "我的",
        "icon": "👤",
        "selectedIcon": "📇"
      }
    ],
    
    // 创意发布模板
    createTemplates: [
      {
        id: 'help',
        title: '求助',
        icon: '🆘',
        color: '#FF5722',
        description: '寻求帮助，快速解决问题',
        url: '/pages/edit/index?template=help'
      },
      {
        id: 'activity',
        title: '活动',
        icon: '🎉',
        color: '#FF9800',
        description: '组织聚会，结识新朋友',
        url: '/pages/edit/index?template=activity'
      },
      {
        id: 'guide',
        title: '攻略',
        icon: '📋',
        color: '#2196F3',
        description: '分享经验，实用指南',
        url: '/pages/edit/index?template=guide'
      },
      {
        id: 'moment',
        title: '瞬间',
        icon: '📸',
        color: '#9C27B0',
        description: '记录美好，即时分享',
        url: '/pages/edit/index?template=moment'
      }
    ]
  },
  
  attached() {
    // 组件初始化时设置正确的选中状态
    this.updateSelectedIndex();
  },
  
  methods: {
    // 根据当前页面路径更新选中的 tab 索引
    updateSelectedIndex() {
      const pages = getCurrentPages();
      if (pages.length === 0) return;
      
      const currentPage = pages[pages.length - 1];
      const currentRoute = currentPage.route;
      
      // 查找匹配的 tab 索引
      const matchIndex = this.data.list.findIndex(item => {
        return item.pagePath === currentRoute;
      });
      
      if (matchIndex !== -1) {
        this.setData({
          selected: matchIndex
        });
      }
    },
    
    // 设置选中的 tab（供页面调用）
    setSelectedIndex(index) {
      this.setData({
        selected: index
      });
    },
    
    switchTab(e) {
      const data = e.currentTarget.dataset;
      const index = parseInt(data.index);
      const item = this.data.list[index];
      
      // 如果是创建按钮，显示发布面板
      if (item.isCreate) {
        this.setData({
          showCreatePanel: true
        });
        return;
      }
      
      // 检查页面路径是否有效
      if (!item.pagePath) {
        console.warn('无效的页面路径:', item);
        return;
      }
      
      const url = '/' + item.pagePath;
      
      // 先更新选中状态
      this.setData({
        selected: index
      });
      
      // 跳转页面
      wx.switchTab({
        url,
        success: () => {
          console.log('成功切换到:', item.pagePath);
        },
        fail: (err) => {
          console.error('Tab切换失败:', err);
          // 切换失败时恢复之前的选中状态
          this.updateSelectedIndex();
        }
      });
    },
    
    // 关闭创建面板
    hideCreatePanel() {
      this.setData({
        showCreatePanel: false
      });
    },
    
    // 选择创建模板
    selectTemplate(e) {
      const template = e.currentTarget.dataset.template;
      const url = template.url;
      
      this.hideCreatePanel();
      
      // 跳转到编辑页面并传递模板参数
      wx.navigateTo({
        url,
        success: () => {
          console.log('跳转到编辑页面:', template.title);
        },
        fail: (err) => {
          console.error('跳转失败:', err);
        }
      });
    },
    
    // 阻止面板内部点击事件冒泡
    stopPropagation() {
      // 空函数，用于阻止事件冒泡
    }
  }
});